﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using PortalCurriculo.Domain;
using PortalCurriculo.WebUI.Helpers;

namespace PortalCurriculo.WebUI.UserControls.Vaga
{
    public partial class Questionarios : UserControlHelper
    {
        public bool Edicao { get; set; }
        public int IdVaga { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            pnlEdicao.Visible = Edicao;
            PreencherQuestionario();

            if (IdVaga == 0) return;

            PreencherListView();
        }

        private void PreencherQuestionario()
        {
            var idEmpresa = new Domain.Vaga().SelectById<Domain.Vaga>(IdVaga).IdEmpresa;
            WebHelper.FillDropDownList(ddlQuestionarios, new Questionario { IdEmpresa = idEmpresa }.SelecionarPorEmpresa(),
                                       true, "Nome");
        }

        protected void lsvQuestionario_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            var item = (ListViewDataItem)e.Item;
            var vagaQuestionario = (VagaQuestionario)item.DataItem;

            ((Label)item.FindControl("lblQuestionario")).Text = vagaQuestionario.Questionario.Nome;
            int? tempoTotal = vagaQuestionario.Questionario.Questaos.Sum(questao => questao.Tempo);

            if (tempoTotal.HasValue)
                ((Label)item.FindControl("lblTempoTotal")).Text = TimeHelper.ConverterMinutos(tempoTotal.Value, AppRelativeVirtualPath);

            var excluir = (ImageButton)item.FindControl("imbExcluir");
            excluir.Visible = Edicao;
            excluir.Attributes.Add("Id", vagaQuestionario.Id.ToString());

            var responder = (LinkButton)item.FindControl("lnkResponder");
            responder.Visible = ProfissionalLogado != null;
            responder.Attributes.Add("Id", vagaQuestionario.Id.ToString());
        }

        protected void imbExcluir_Click(object sender, ImageClickEventArgs e)
        {
            int id = int.Parse(((ImageButton)sender).Attributes["Id"]);
                
            new VagaQuestionario().SelectById<VagaQuestionario>(id).Inative();

            PreencherListView();
        }

        private void PreencherListView()
        {
            lsvQuestionario.DataSource = new VagaQuestionario { IdVaga = IdVaga }.SelecionarPorVaga();
            lsvQuestionario.DataBind();
        }

        protected void lnkAdicionar_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid) return;

            var vagaQuestionario = new VagaQuestionario
                                       {
                                           IdVaga = IdVaga,
                                           IdQuestionario = int.Parse(ddlQuestionarios.SelectedValue),
                                           Obrigatorio = chkObrigatorio.Checked
                                       };

            vagaQuestionario.Save();

            LimparCampos();
            PreencherListView();
            WebHelper.ShowMessageBox("Questionário incluído com sucesso!", ref uppQuestionarios);

            ddlQuestionarios.Focus();
        }

        private void LimparCampos()
        {
            ddlQuestionarios.SelectedIndex = 0;
            chkObrigatorio.Checked = false;
        }
    }
}